package top.lv666.graduation_design_serve.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lv666.graduation_design_serve.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/other")
public class OtherController {
    // log4j
    public Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * @apiNote 获取邮箱验证码
     */
    @PostMapping("/getEmailCode")
    public void createCode(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        try {
            map.put("request", request);
            userService.getCode(map);
            logger.info("【获取邮箱验证码】" + map);
        } catch (Error | Exception e) {
            //写入日志
            logger.error("【USER】邮件发送失败！" + e);
        }
    }

    /**
     * @apiNote 获取手机验证码
     */
    @PostMapping("/getPhoneCode")
    public Map<String, Object> getPhoneCode(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        //添加请求体
        map.put("request", request);
        Map<String, Object> returnMap = new HashMap<>();
        try {
            userService.getPhoneCode(map);
            returnMap.put("flag", true);
            logger.info("【手机验证码获取成功】" + map);
        } catch (Error | Exception e) {
            returnMap.put("flag", false);
            logger.error("【手机验证码获取失败】" + e.getMessage());
        }
        return returnMap;
    }

}
